/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.memberView;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.dianlian.pss.inter.ws.UserMgr;
import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TMyMessage;
import com.ruipengkj.bean.TUser;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJTextAreaComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.service.TMyMessageService;
import com.ruipengkj.view.base.BaseDialog;

/**
 * 说明：
 * 会员详细Dialog
 * @Author：李智深
 * @Date：2012-03-20
 */
public class MemberNoticeDetailDialog extends BaseDialog{

	private static final long serialVersionUID = 1L;

	private JLabelJTextAreaComponent content = null; //添加通知内容
	private TMyMessageService tMyMessageService;
	private JButton sureButton = null; //确定按钮
	private JButton cancelButton = null; //取消
	List<TUser> tUser = new ArrayList();
	public MemberNoticeDetailDialog(JDialog jd, String title, boolean b) {	
		super(jd, title, b);
		tMyMessageService = BuildSpringApplication.getInstance().getTMyMessageServiceImpl();
		this.init();
		this.setSize(300, 550);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
	}
	private void init(){
		this.initViews();
		this.initListener();
	}
	
	private void initViews(){
		JPanel main_jp=new JPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp,BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		main_jp.setBackground(Util.PANEL_COLOR);
		
		content=new JLabelJTextAreaComponent("通知内容:",22,3);
		JPanel jp1=new JPanel();
		sureButton=new RPJButtonBlue("确定");
		cancelButton = new RPJButtonGray("取消");
		jp1.add(sureButton);
		jp1.add(cancelButton);
		jp1.setOpaque(false);
		
		main_jp.add(content);
		main_jp.add(jp1);
		
		this.add(main_jp);
	}
	private void initListener(){
		sureButton.addActionListener(this);
		sureButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_Y, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		cancelButton.addActionListener(this);
		//fillMoneyTF.getText().addKeyListener(this);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
//		if(e.getSource()==sureButton){
//			Boolean isShow =false;
//			//确定
//			String contents=content.getArea().getText().trim(); //充值金额
//			if(contents.equals("") || contents==null){
//				JOptionPane.showMessageDialog(null, "通知内容不能为空！");
//				content.getArea().setText("");
//				content.getArea().requestFocus();
//				return;
//			}
//			for(int i =0 ; i < tUser.size(); i++){
//				try {
//					Date date = new Date();
//						TMyMessage tMyMessage = new TMyMessage();//添加通知
//						tMyMessage.setContent(contents);
//						tMyMessage.setMyMessageID(UUID.GenUUID15());
//						tMyMessage.setRelatedID(ApplicationContext.OPERATOR_ID);
//						tMyMessage.setSendTime(date);
//						tMyMessage.setStatus((byte)1);
//						tMyMessage.setTUser(tUser.get(i));
//						tMyMessage.setType((byte)21);
//						UserMgr um = ApplicationContext.conn.getSiteUserMgr();
//						boolean r;
//						r = um.notice(tUser.get(i).getUserID(), tMyMessage.getContent(), UserMgr.NOTICE_LEVEL_IMME);
//
//						tMyMessageService.saveService(tMyMessage);
//						isShow=true;
//				} catch (Exception e1) {
//					isShow=false;
//					e1.printStackTrace();
//				}
//				
//			}
//			if(isShow){
//				JOptionPane.showMessageDialog(null, "通知发送成功!");
//				this.dispose();
//			}else{
//				JOptionPane.showMessageDialog(null, "数据异常。");
//			}
//			//table.updateUI();
//		}else if(e.getSource()==cancelButton){
//			this.dispose();
//		}
		if(e.getSource()==sureButton){
			Boolean isShow =false;
			//确定
			String contents=content.getArea().getText().trim(); //充值金额
			if(contents.equals("") || contents==null){
				JOptionPane.showMessageDialog(null, "通知内容不能为空！");
				content.getArea().setText("");
				content.getArea().requestFocus();
				return;
			}
			for(int i =0 ; i < tUser.size(); i++){
				try {
					Date date = new Date();
						TMyMessage tMyMessage = new TMyMessage();//添加通知
						tMyMessage.setContent(contents);
						tMyMessage.setMyMessageID(UUID.GenUUID15());
						tMyMessage.setRelatedID(ApplicationContext.OPERATOR_ID);
						tMyMessage.setSendTime(date);
						tMyMessage.setStatus((byte)1);
						tMyMessage.setTUser(tUser.get(i));
						tMyMessage.setType((byte)21);
						UserMgr um = ApplicationContext.conn.getSiteUserMgr();
						boolean r;
						r = um.notice(tUser.get(i).getUserID(), tMyMessage.getContent(), UserMgr.NOTICE_LEVEL_IMME);

						tMyMessageService.saveService(tMyMessage);
						isShow=true;
				} catch (Exception e1) {
					isShow=false;
					e1.printStackTrace();
				}
				
			}
			if(isShow){
				JOptionPane.showMessageDialog(null, "通知发送成功!");
				this.dispose();
			}else{
				JOptionPane.showMessageDialog(null, "数据异常。");
			}
			//table.updateUI();
		}else if(e.getSource()==cancelButton){
			this.dispose();
		}
	}
	public void showDialog(List<TUser> user){
		for(int i =0;i<user.size();i++){
			tUser.add(user.get(i));
		}
		this.setVisible(true);
	}
}
